//
//  MonsterView.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/9.
//
//

#include "MonsterView.h"
#include "AttackAction.h"
#include "HeroUsed.h"


USING_NS_CC;

#define WIDTH  320
#define HEIGHT 168
#define ATTACK_GAP_TIME 0.5
#define MAX_ATTACK_NUM  100
#define ACTION_X_PAD    (2)
#define ACTION_Y_PAD    (2)

typedef enum {
    ZBg = 0,
    ZMonster,
    ZAttackAction,
} Zindex;

MonsterView::MonsterView()
:monster(NULL)
,attackArray(NULL)
,lastTime(ATTACK_GAP_TIME)
,m_isPause(false)
{
}

MonsterView::~MonsterView()
{
    attackArray->removeAllObjects();
    CC_SAFE_RELEASE_NULL(attackArray);
}

MonsterView* MonsterView::create(int type)
{
    MonsterView *pRet = new(std::nothrow) MonsterView();
    if (pRet && pRet->init(type)){
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

bool MonsterView::init(int type)
{
    if (!Layer::init()) {
        return false;
    }
    attackArray = __Array::createWithCapacity(MAX_ATTACK_NUM);
    attackArray->retain();
    bgInit(type);
    monsterInit(type);
    scheduleUpdate();
    
    return true;
}

void MonsterView::update(float dt)
{
	if(!m_isPause){
	    if(lastTime > 0){
	        lastTime -= dt;
	    }else{
	        lastTime = ATTACK_GAP_TIME;
	        checkMonsterBeAttack();
	    }
	}
}

void MonsterView::checkMonsterBeAttack()
{
    if(attackArray->count() > 0){
    	MonsterBeAttackRecorder *recorder = (MonsterBeAttackRecorder*)(attackArray->getLastObject());
        showAction(recorder->getType());
        int damage = HeroUsed::getInstance()->getDamageByType(recorder->getType());
        if(monster->changeHealth(damage,recorder->getType(),recorder->getDoubleNum())){
            _callback(this);
        }
        attackArray->removeLastObject();
    }
}

void MonsterView::bgInit(int type)
{
    Sprite* bg = Sprite::create(MonsterConfig::getBgName(type));
    bg->setAnchorPoint(Point(0, 0));
    bg->setPosition(Point(0, 0));
    addChild(bg);
}

void MonsterView::monsterInit(int type)
{
    monster = Monster::create(type);
    monster->setPosition(Point((WIDTH - monster->getSize().width)/2,
                               (HEIGHT - monster->getSize().height)/2));
    monster->setCallback(CC_CALLBACK_1(MonsterView::monsterViewBossAttackCallbackFun, this));
    addChild(monster,ZMonster);

}

void MonsterView::monsterViewBossAttackCallbackFun(Ref *sender)
{
	_bossAttackCallback(this);
}

Size MonsterView::getSize()
{
    return Size(WIDTH,HEIGHT);
}

void MonsterView::beAttacked(MonsterBeAttackRecorder *recorder)
{
    attackArray->addObject(recorder);
}

void MonsterView::showAction(int type)
{
    auto attack = AttackAction::create(type);
    attack->setPosition(Point(monster->getPosition().x +ACTION_X_PAD,
                              monster->getPosition().y +ACTION_Y_PAD));
    addChild(attack,ZAttackAction);
}

void MonsterView::setCallback(const monsterViewCallback& callback)
{
    _callback = callback;
}

void MonsterView::beHealthed(int num)
{
    monster->addHealth(num);
}

void MonsterView::setBossAttackCallback(const monsterViewBossAttackCallback& callback)
{
	_bossAttackCallback = callback;
}

void MonsterView::setPause(bool isPause)
{
	m_isPause = isPause;

}

void MonsterView::destoryMonsterProtectCover()
{
	monster->destoryProtectCover();
}

